//
//  RealmHelper.swift
//  UICPortal
//
//  Created by 高宇超 on 7/21/17.
//  Copyright © 2017 Yuchao. All rights reserved.
//

import RealmSwift

class RealmHelper {

    
    // MARK: - User
    static func addUser(_ user: UserRealm) {
        let realm = try! Realm()
        try! realm.write {
            realm.add(user)
        }
    }

    static func updatePassword(_ user: UserRealm, newPassword password: String, last: Bool = true) {
        let realm = try! Realm()
        try! realm.write {
            user.password = password
            user.last = true
        }
    }
    
    static func updateStu(_ user: UserRealm, avStu stu: AVStudent, last: Bool = true) {
        let realm = try! Realm()
        try! realm.write {
            user.avID = stu.objectId!
            user.name = stu.name!
            user.stuID = stu.stuID!
            user.nameEng = stu.nameEng!
            user.program = stu.program!
            user.address = stu.address!
            user.birthDate = stu.birthDate
            user.mobilePhone = stu.mobilePhone!
            user.homePhone = stu.homePhone!
            user.postcode = stu.postcode!
            user.idCardNo = stu.idCardNo!
            user.gender = stu.gender
            user.last = true
            user.photoData = stu.photo?.getData()
        }
    }
    
    static func updateGender(_ user: UserRealm, _ gender: String) {
        let realm = try! Realm()
        try! realm.write {
            user.gender = gender
        }
    }
    
    static func logoutLastUser(_ user: UserRealm) {
        let realm = try! Realm()
        try! realm.write {
            user.last = false
        }
    }
    
    static func retrieveLastUser() -> UserRealm? {
        let realm = try! Realm()
        return realm.objects(UserRealm.self).filter("#last = true").first
    }
    
    static func retrieveUser(byUsername username: String) -> UserRealm? {
        let realm = try! Realm()
        return realm.object(ofType: UserRealm.self, forPrimaryKey: username)
    }
    
    
    // MARK: - Class
    static func addClass(_ classToAdd: ClassRealm) {
        let realm = try! Realm()
        try! realm.write {
            realm.add(classToAdd)
        }
    }
    
    static func addClassOne(_ classToAdd: ClassOneRealm) {
        let realm = try! Realm()
        try! realm.write {
            realm.add(classToAdd)
        }
    }
    
    static func setClassOneCourse(_ classToSet: ClassOneRealm, course: ClassRealm) {
        let realm = try! Realm()
        try! realm.write {
            classToSet.course = course
        }
    }
    
    static func setClassOne(classOneRealm: ClassOneRealm, minutes: Int, endTime: Int) {
        let realm = try! Realm()
        try! realm.write {
            classOneRealm.minutes = minutes
            classOneRealm.endTime = endTime
        }
    }
    
    static func retrieveClass(by title: String, user: String) -> ClassRealm? {
        let realm = try! Realm()
        return realm.objects(ClassRealm.self).filter("title = '\(title)'").filter("user = '\(user)'").first
    }

    static func retrieveClass(by weekday: String, _ dateTime: String, user: String) -> ClassOneRealm? {
        let realm = try! Realm()
        return realm.objects(ClassOneRealm.self).filter("weekday = '\(weekday)' && dateTime = '\(dateTime)'").filter("user = '\(user)'").first
    }
    
    static func retrieveAllClasses(user: String) -> Results<ClassRealm> {
        let realm = try! Realm()
        return realm.objects(ClassRealm.self).filter("user = '\(user)'")
    }
    
    static func removeAllClass() {
        let realm = try! Realm()
        try! realm.write {
            realm.delete(realm.objects(ClassRealm.self))
            realm.delete(realm.objects(ClassOneRealm.self))
        }
    }
    
    
}
